//
//  WHNHistoryCollectionViewCell.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WHNHistoryCollectionViewCell.h"
#import "WHNTableViewCell.h"
#import "PlayHistory.h"
#import <UIImageView+WebCache.h>
#import "PlayerButton.h"
#import <MBProgressHUD.h>
#import "History.h"
#import "FPlayViewController.h"
#import "WHNThirdViewController.h"

@implementation WHNHistoryCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _manager = [CoreDataManager shareManager];
        _dataSource = [NSArray array];
        _playHistoryArrary = [NSArray array];
        [self selectAction];
        [self playHistory];
        [self addSubview:self.tableView];
        self.tableView.tableFooterView = [[UIView alloc] init];
    }
    return self;
}

////查找数据库里所有历史
//-(void)selectAction
//{
//    //创建查询请求对象
//    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"PlayHistory"];
//    //查询结果排序方式
//    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
//    [fetchRequest setSortDescriptors:@[sortDescriptor]];
//    //将请求对象发给manager
//    _dataSource = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
//
//}

#pragma mark - 加载tableView
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView NightWithType:UIViewColorTypeNormal];
    }
    return _tableView;
}
//-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    static NSString *mark = @"cell";
//    WHNTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mark];
//    if (!cell) {
//        cell = [[WHNTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:mark];
//        cell.kind = WHNTableViewCellKindThree;
//        //NSLog(@"%@", [_dataSource[indexPath.row]trackID]);
//    
//    }
//    
//    cell.backgroundColor = [UIColor redColor];
//    return cell;
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 100;
//}
//-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return _dataSource.count;
//}

#pragma mark 加载提示Label
-(UILabel *)errorLabel
{
    if (!_errorLabel) {
        _errorLabel = [[UILabel alloc] initWithFrame:FitRectMake(50, 50, 375, 40)];
        [_errorLabel NightWithType:UIViewColorTypeNormal];
        _errorLabel.text = @"没有历史记录";
    }
    return _errorLabel;
}


#pragma mark -查找数据库里所有历史
#pragma mark 电台历史
-(void)selectAction
{
    _dataSource = nil;
//    //创建查询请求对象
//    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"History"];
//    //查询结果排序方式
//    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
//    [fetchRequest setSortDescriptors:@[sortDescriptor]];
//    //将请求对象发给manager
//    _dataSource = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
}

#pragma mark 播放历史
-(void)playHistory
{
    //创建查询请求对象
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"PlayHistory"];
    //查询结果排序方式
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
    [fetchRequest setSortDescriptors:@[sortDescriptor]];
    //将请求对象发给manager
    _playHistoryArrary = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    [self.tableView reloadData];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *mark = @"cell";
    WHNTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mark];
    
    if (!cell) {
        cell = [[WHNTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:mark];
        cell.kind = WHNTableViewCellKindSix;
    }
    if (indexPath.section == 0) {
        if (_playHistoryArrary.count != 0) {
            PlayHistory *history = _playHistoryArrary [indexPath.row];
            [cell.MainImageView sd_setImageWithURL:[NSURL URLWithString: history.imageUrl] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
            cell.TitleLabel.text = history.title;
        }
    }
    else{
        if (_dataSource.count != 0) {
            History *history = _dataSource [indexPath.row];
            [cell.MainImageView sd_setImageWithURL:[NSURL URLWithString: history.imageUrl] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
            cell.TitleLabel.text = history.title;
        }
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataSource.count == 0 && _playHistoryArrary.count == 0) {
        [self addSubview:self.errorLabel];
        return 0;
    }
    if (section == 0) {
        return _playHistoryArrary.count;
    }
    else{
        return _dataSource.count;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc] initWithFrame:FitRectMake(0, 0, 375, 40)];
    [label NightWithType:UIViewColorType3];
    if (_dataSource.count != 0 || _playHistoryArrary.count != 0) {
        if (section == 0) {
            label.text = @"      播放历史";
        }
        else{
//            label.text = @"      电台播放历史";
        }
    }
    return label;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FitConstant_YorHeight(40);
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        PlayHistory *histroy = _playHistoryArrary[indexPath.row];
        [self.delegate didSelect:histroy Section:indexPath.section];
    }
    else{
        History *histroy = _dataSource[indexPath.row];
        [self.delegate didSelect:histroy Section:indexPath.section];
    }
    
}


@end
